package model.instrutor;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.banco.ConexaoFactory;
import model.pessoa.Aluno;
import model.pessoa.Instrutor;

public class InstrutorDAO {
	
	private Connection connection;
	
	public InstrutorDAO(){
		connection = new ConexaoFactory().getConnection();
	}
	
	public void salvarAluno(Instrutor instrutor) {
		String sql = "INSERT INTO ACADEMIA.INSTRUTOR (MATRICULA, NOME, TELEFONE, TITULACAO, RG, CPF, DATA_NASCIMENTO) VALUES (?, ?, ?, ?, ?, ?)";
		//String sqlEndereco = "INSERT INTO ENDERECO "
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			statement.setString(1, instrutor.getMatricula());
			statement.setString(2, instrutor.getNome());
			statement.setString(3, instrutor.getTelefone());
			statement.setString(4, instrutor.getTitulacao());
			statement.setString(5, instrutor.getRg());
			statement.setString(6, instrutor.getCpf());
			statement.setString(7, instrutor.getDataNascimento());
			statement.setString(8, instrutor.getEndereco());
			statement.execute();
			statement.close();
			connection.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	
	
	public List<Instrutor> pesquisarInstrutor(String matricula, String nome) {
		
		String sql = "select id, matricula, nome, rg, cpf from instrutor where codigo like '%" + matricula + "%' and descricao like '%" + nome + "%'";
		
		try {
			PreparedStatement statement = connection.prepareStatement(sql);
			
			ResultSet rs = statement.executeQuery();
			
			List<Instrutor> alunos = new ArrayList<Instrutor>();
			
			while(rs.next()){
				Instrutor instrutor = new Instrutor();
				
				instrutor.setId(rs.getLong("id"));
				instrutor.setMatricula(rs.getString("matricula"));
				instrutor.setNome(rs.getString("nome"));
				instrutor.setRg(rs.getString("rg"));
				instrutor.setCpf(rs.getString("cpf"));
				
			}
			
			rs.close();
			statement.close();
			return alunos;
			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		

		return null;
	}
	
	public static void main(String[] args) {
		
	}

}
